Back to course home
0% completed
Vote For New Content
Microsoft Hard Problems
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Spiral Matrix (medium)
Problem Statement
Given a 2D matrix of size m x n, return the 1D array containing all elements of matrix in spiral order.
Examples
Example 1:
- Input: matrix =
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]
- Expected Output:
[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10] - Justification: We have traversed the matrix in the spiral order.
Example 2:
- Input: matrix =
[[10,20,30],
[40,50,60],
[70,80,90]]
- Expected Output:
[10,20,30,60,90,80,70,40,50] - Justification: The traversal starts at the top-left, moves right to the end of the row, then down the right column, then left along the bottom row, up the left column, and finally captures the center.
Example 3:
- Input: matrix =
[[1,2],
[3,4],
[5,6]]
- Expected Output:
[1,2,4,6,5,3] - Justification: The traversal starts at the top-left, moves right, then down through the right column, and finally moves up the left column.
Constraints:
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Distribute Coins in Binary Tree
Solution: Spiral Matrix
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself